{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled35.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iUm9rNk51gE1",
        "colab_type": "text"
      },
      "source": [
        "<!--COURSE_INFORMATION-->\n",
        "<img align=\"left\" style=\"padding-right:10px;\" src=\"https://sitejerk.com/images/google-earth-logo-png-5.png\" width=5% >\n",
        "<img align=\"right\" style=\"padding-left:10px;\" src=\"https://colab.research.google.com/img/colab_favicon_256px.png\" width=6% >\n",
        "\n",
        "\n",
        ">> *This notebook is part of the free course [EEwPython](https://colab.research.google.com/github/csaybar/EEwPython/blob/master/index.ipynb); the content is available [on GitHub](https://github.com/csaybar/EEwPython)* and released under the [Apache 2.0 License](https://www.gnu.org/licenses/gpl-3.0.en.html). 99% of this material has been adapted from [Google Earth Engine Guides](https://developers.google.com/earth-engine/)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4A7t6GOL1izU",
        "colab_type": "text"
      },
      "source": [
        "<!--NAVIGATION-->\n",
        " < [Array](8_Array.ipynb) | [Contents](index.ipynb) |  [Asset Management](10_Import.ipynb)>\n",
        "\n",
        "<a href=\"https://colab.research.google.com/github/csaybar/EEwPython/blob/master/9_SpecializedAlgorithms.ipynb\"><img align=\"left\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\" title=\"Open and Execute in Google Colaboratory\"></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Q5NpPVbY1kjZ",
        "colab_type": "text"
      },
      "source": [
        "<center>\n",
        "<h1>Google Earth Engine with Python </h1>\n",
        "<h2> Specialized Algorithms </h2>\n",
        "</center>\n",
        "<h2> Topics:</h2>\n",
        "\n",
        "1. Supervised Classification\n",
        "2. Unsupervised Classification (clustering)\n",
        "3. Landsat Algorithms\n",
        "4. Sentinel-1 Algorithms\n",
        "5. Resampling and Reducing Resolution\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AZhRndu_klmI",
        "colab_type": "text"
      },
      "source": [
        "## Connecting GEE with Google Services"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tqpqSXHI1qqO",
        "colab_type": "text"
      },
      "source": [
        "- **Authenticate to Earth Engine**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wktUBlhE1q1I",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install earthengine-api #earth-engine Python API"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wZ2lJ0-1kvsm",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!earthengine authenticate "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZO7GiOh01rtV",
        "colab_type": "text"
      },
      "source": [
        "- **Authenticate to Google Drive (OPTIONAL)**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "99Dbrd431spS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vWoEP8EfkziT",
        "colab_type": "text"
      },
      "source": [
        "- **Authenticate to Google Cloud (OPTIONAL)**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CNnxj6Pg1upy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import auth\n",
        "auth.authenticate_user()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0de8jZQik3eY",
        "colab_type": "text"
      },
      "source": [
        "## Testing the software setup"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UEpU8v_n1vx5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Earth Engine Python API\n",
        "import ee\n",
        "ee.Initialize()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8xADmFcpk56q",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import folium\n",
        "\n",
        "# Define the URL format used for Earth Engine generated map tiles.\n",
        "EE_TILES = 'https://earthengine.googleapis.com/map/{mapid}/{{z}}/{{x}}/{{y}}?token={token}'\n",
        "\n",
        "print('Folium version: ' + folium.__version__)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xGp18_Xn7bNO",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "#@title Mapdisplay: Display GEE objects using folium.\n",
        "def Mapdisplay(center, dicc, Tiles=\"OpensTreetMap\",zoom_start=10):\n",
        "    '''\n",
        "    :param center: Center of the map (Latitude and Longitude).\n",
        "    :param dicc: Earth Engine Geometries or Tiles dictionary\n",
        "    :param Tiles: Mapbox Bright,Mapbox Control Room,Stamen Terrain,Stamen Toner,stamenwatercolor,cartodbpositron.\n",
        "    :zoom_start: Initial zoom level for the map.\n",
        "    :return: A folium.Map object.\n",
        "    '''\n",
        "    mapViz = folium.Map(location=center,tiles=Tiles, zoom_start=zoom_start)\n",
        "    for k,v in dicc.items():\n",
        "      if ee.image.Image in [type(x) for x in v.values()]:\n",
        "        folium.TileLayer(\n",
        "            tiles = EE_TILES.format(**v),\n",
        "            attr  = 'Google Earth Engine',\n",
        "            overlay =True,\n",
        "            name  = k\n",
        "          ).add_to(mapViz)\n",
        "      else:\n",
        "        folium.GeoJson(\n",
        "        data = v,\n",
        "        name = k\n",
        "          ).add_to(mapViz)\n",
        "    mapViz.add_child(folium.LayerControl())\n",
        "    return mapViz"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Vh15H8Ta2T0I",
        "colab_type": "text"
      },
      "source": [
        "# 1. Supervised Classification\n",
        "\n",
        "The `Classifier` package handles supervised classification in Earth Engine. The general workflow for classification is:\n",
        "\n",
        "1. Collect training data. Assemble features which have a property that stores the known class label and properties storing numeric values for the predictors.\n",
        "\n",
        "2. Instantiate a classifier. Set its parameters if necessary.\n",
        "3. Train the classifier using the training data.\n",
        "4. Classify an image or feature collection.\n",
        "5. Estimate classification error with independent validation data.\n",
        "\n",
        "The training data is a `FeatureCollection` with a property storing the class label and properties storing predictor variables. Class labels should be small, consecutive, non-negative integers representing classes. If necessary, use `remap()` to convert class values to consecutive integers starting from 0. The predictors should be numeric.\n",
        "\n",
        "\n",
        "Training and/or validation data can come from a variety of sources. To collect training data interactively in Earth Engine, you can use the geometry drawing tools (see the [Code Editor section](https://developers.google.com/earth-engine/playground)). Alternatively, you can import predefined training data from an Earth Engine table asset or a Fusion Table (see the [Importing section](https://developers.google.com/earth-engine/importing) for details). Get a classifier from one of the constructors in `ee.Classifier`. Train the classifier using `classifier.train()`. Classify an `Image` or `FeatureCollection` using `classify()`. The following example uses a Classification and Regression Trees (CART) classifier ([Breiman et al. 1984](https://books.google.com.pe/books?id=JwQx-WOmSyQC&redir_esc=y)) to predict forest and non-forest areas in the Amazon:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hP3BhT-v2Ux8",
        "colab_type": "code",
        "outputId": "583aeb37-64bc-411c-a1ac-34b7ae39ae5e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Use these bands for prediction.\n",
        "bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];\n",
        "\n",
        "# Load a Landsat 8 image to be used for prediction.\n",
        "image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_232067_20130726')\\\n",
        "          .select(bands)\n",
        "\n",
        "# Load training points. The numeric property 'class' stores known labels.\n",
        "points = ee.FeatureCollection('ft:10X7SUjDTiFJDyIA58zLcptK8pwBwjj1BV12SQOgJ')\\\n",
        "           .remap([1, 2], [0, 1], 'class')\n",
        "\n",
        "# Overlay the points on the imagery to get training.\n",
        "training = image.sampleRegions(\n",
        "  collection=points,\n",
        "  properties=['class'],\n",
        "  scale=30)\n",
        "\n",
        "\n",
        "# Train a CART classifier with default parameters.\n",
        "trained = ee.Classifier.cart().train(training, 'class', bands)\n",
        "\n",
        "# Classify the image with the same bands used for training.\n",
        "classified = image.select(bands).classify(trained)\n",
        "\n",
        "# Display the inputs and the results.\n",
        "center = points.geometry().centroid().getInfo()['coordinates']\n",
        "center.reverse()\n",
        "\n",
        "image_vizparam = {'bands': ['B4', 'B3', 'B2'], 'max': 0.4}\n",
        "image_token = image.getMapId(image_vizparam)\n",
        "\n",
        "classify_vizparam = {'min': 0, 'max': 1, 'palette': ['00FF00', 'FF0000']}\n",
        "classify_token = classified.getMapId(classify_vizparam)\n",
        "\n",
        "Mapdisplay(center = center,\n",
        "           dicc = {'LANDSAT':image_token,\n",
        "                   'FOREST':classify_token},\n",
        "           zoom_start=8)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL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AiYWJjIiwKICAgICAgICAidG1zIjogZmFsc2UKfSkuYWRkVG8obWFwX2UzZTA4NWM4ODlhOTRjMzU5YTk3MWQ4MzQ4NGVhNmEwKTsKICAgIAogICAgICAgICAgICB2YXIgbGF5ZXJfY29udHJvbF84MTcxY2Y1OWUxNmM0MDllYTlmNTEwZmRlZWJhZDMyZiA9IHsKICAgICAgICAgICAgICAgIGJhc2VfbGF5ZXJzIDogeyAib3BlbnN0cmVldG1hcCIgOiB0aWxlX2xheWVyXzI5N2FmMmY2ODM1YzQwZjdiMWMxYzQ2NTc0ZWJlNmU2LCB9LAogICAgICAgICAgICAgICAgb3ZlcmxheXMgOiB7ICJMQU5EU0FUIiA6IHRpbGVfbGF5ZXJfMzMyMzNjYjEzZWM5NDcxOWJiMjdiNmQ5ZmI3OGIyM2EsIkZPUkVTVCIgOiB0aWxlX2xheWVyXzBjY2FhMTU4MDE3NDQxNjA5MWY2NThlN2E4MDBmYmI1LCB9CiAgICAgICAgICAgICAgICB9OwogICAgICAgICAgICBMLmNvbnRyb2wubGF5ZXJzKAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF84MTcxY2Y1OWUxNmM0MDllYTlmNTEwZmRlZWJhZDMyZi5iYXNlX2xheWVycywKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfODE3MWNmNTllMTZjNDA5ZWE5ZjUxMGZkZWViYWQzMmYub3ZlcmxheXMsCiAgICAgICAgICAgICAgICB7cG9zaXRpb246ICd0b3ByaWdodCcsCiAgICAgICAgICAgICAgICAgY29sbGFwc2VkOiB0cnVlLAogICAgICAgICAgICAgICAgIGF1dG9aSW5kZXg6IHRydWUKICAgICAgICAgICAgICAgIH0pLmFkZFRvKG1hcF9lM2UwODVjODg5YTk0YzM1OWE5NzFkODM0ODRlYTZhMCk7CiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f4f5ef28>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 63
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oL_YHddwEKD6",
        "colab_type": "text"
      },
      "source": [
        "In this example, the [training points in the Fusion Table](https://fusiontables.google.com/DataSource?docid=10X7SUjDTiFJDyIA58zLcptK8pwBwjj1BV12SQOgJ#rows:id=1) store only the class label. Note that `remap()` is used to convert the training property to consecutive integers starting at 0. Also note the use of `image.sampleRegions()` to get the predictors into the table and create a training dataset. To train the classifier, specify the name of the class label property and a list of properties in the training table which the classifier should use for predictors. The number and order of the bands in the image to be classified must exactly match the order of the properties list provided to `classifier.train()`. Use `image.select()` to ensure that the classifier schema matches the image.\n",
        "\n",
        "If the training data are polygons representing homogenous regions, every pixel in each polygon is a training point. Use polygons to train as illustrated in the following example:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gQL8IWih7O6F",
        "colab_type": "code",
        "outputId": "ab14b8eb-aa16-4aff-baf1-6f1fd1f64756",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Use these bands for prediction.\n",
        "bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11']\n",
        "\n",
        "# Load an image over a portion of southern California, USA.\n",
        "image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_040037_20131114')\\\n",
        "          .select(bands)\n",
        "\n",
        "# Load training polygons from a Fusion Table.\n",
        "# The 'class' property stores known class labels.\n",
        "polygons = ee.FeatureCollection('ft:1vYn7-uO80vAVpZxi81yXzx55jgtztsZaLxG5TWYH')\n",
        "\n",
        "# Get the values for all pixels in each polygon in the training.\n",
        "training = image.sampleRegions(\n",
        "  collection=polygons,\n",
        "  properties=['class'],\n",
        "  scale=30)\n",
        "\n",
        "# Create an SVM classifier with custom parameters.\n",
        "classifier = ee.Classifier.svm(\n",
        "  kernelType='RBF',\n",
        "  gamma=0.5,\n",
        "  cost=10)\n",
        "\n",
        "# Train the classifier.\n",
        "trained = classifier.train(training, 'class', bands)\n",
        "\n",
        "# Classify the image.\n",
        "classified = image.classify(trained)\n",
        "\n",
        "# Create a palette to display the classes.\n",
        "palette =['006400', '32CD32', 'EEE8AA',\n",
        "          '8B4513', '98FB98', '00FA9A',\n",
        "          '90EE90', '00008B', 'FF8C00',\n",
        "          'ADFF2F', '808080']\n",
        "\n",
        "# Display the classification result and the input image.\n",
        "center = [33.30, -117.3]\n",
        "token_image = image.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 0.5, 'gamma': 2})\n",
        "token_classified = classified.getMapId({'min': 0, 'max': 10, 'palette': palette})\n",
        "Mapdisplay(center,{'LANDSAT':token_image,'Vegetation_Type':token_classified},zoom_start=9)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR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kxZTlkMTc0JywKICAgICAgICB7CiAgICAgICAgImF0dHJpYnV0aW9uIjogIkdvb2dsZSBFYXJ0aCBFbmdpbmUiLAogICAgICAgICJkZXRlY3RSZXRpbmEiOiBmYWxzZSwKICAgICAgICAibWF4TmF0aXZlWm9vbSI6IDE4LAogICAgICAgICJtYXhab29tIjogMTgsCiAgICAgICAgIm1pblpvb20iOiAwLAogICAgICAgICJub1dyYXAiOiBmYWxzZSwKICAgICAgICAib3BhY2l0eSI6IDEsCiAgICAgICAgInN1YmRvbWFpbnMiOiAiYWJjIiwKICAgICAgICAidG1zIjogZmFsc2UKfSkuYWRkVG8obWFwX2M2MjJjM2M0NDc0NTRmZTFhYWVmODA3NGY0YzFiOGE0KTsKICAgIHZhciB0aWxlX2xheWVyX2M1YjZlODZlNDI3ODRhNzlhNTlkYWVjNmE2MjEyNTYyID0gTC50aWxlTGF5ZXIoCiAgICAgICAgJ2h0dHBzOi8vZWFydGhlbmdpbmUuZ29vZ2xlYXBpcy5jb20vbWFwL2ExZDExZmYyMzc3YjRmMTZiNGVlNWU1N2FkOWExZjZmL3t6fS97eH0ve3l9P3Rva2VuPTk2ZTc4ZmY3MzNlMzg1N2NhZTk1NWJmZmZkNzhkZmMzJywKICAgICAgICB7CiAgICAgICAgImF0dHJpYnV0aW9uIjogIkdvb2dsZSBFYXJ0aCBFbmdpbmUiLAogICAgICAgICJkZXRlY3RSZXRpbmEiOiBmYWxzZSwKICAgICAgICAibWF4TmF0aXZlWm9vbSI6IDE4LAogICAgICAgICJtYXhab29tIjogMTgsCiAgICAgICAgIm1pblpvb20iOiAwLAogICAgICAgICJub1dyYXAiOiBmYWxzZSwKICAgICAgICAib3BhY2l0eSI6IDEsCiAgICAgICAgInN1YmRvbWFpbnMiOiAiYWJjIiwKICAgICAgICAidG1zIjogZmFsc2UKfSkuYWRkVG8obWFwX2M2MjJjM2M0NDc0NTRmZTFhYWVmODA3NGY0YzFiOGE0KTsKICAgIAogICAgICAgICAgICB2YXIgbGF5ZXJfY29udHJvbF8wYzc1N2JiYTQ4ZmM0MzFjOGVhMjdmOGRjMmFkYjg1MCA9IHsKICAgICAgICAgICAgICAgIGJhc2VfbGF5ZXJzIDogeyAib3BlbnN0cmVldG1hcCIgOiB0aWxlX2xheWVyX2UyMTY5OTkzZWI5NTRmZjc5OGU0OTczYTQ1YjI5ZGM4LCB9LAogICAgICAgICAgICAgICAgb3ZlcmxheXMgOiB7ICJMQU5EU0FUIiA6IHRpbGVfbGF5ZXJfZmY5YTUxNGVlNDhmNGRjYmExNzFlODE5M2Q4ZTE1YmYsIlZlZ2V0YXRpb25fVHlwZSIgOiB0aWxlX2xheWVyX2M1YjZlODZlNDI3ODRhNzlhNTlkYWVjNmE2MjEyNTYyLCB9CiAgICAgICAgICAgICAgICB9OwogICAgICAgICAgICBMLmNvbnRyb2wubGF5ZXJzKAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF8wYzc1N2JiYTQ4ZmM0MzFjOGVhMjdmOGRjMmFkYjg1MC5iYXNlX2xheWVycywKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfMGM3NTdiYmE0OGZjNDMxYzhlYTI3ZjhkYzJhZGI4NTAub3ZlcmxheXMsCiAgICAgICAgICAgICAgICB7cG9zaXRpb246ICd0b3ByaWdodCcsCiAgICAgICAgICAgICAgICAgY29sbGFwc2VkOiB0cnVlLAogICAgICAgICAgICAgICAgIGF1dG9aSW5kZXg6IHRydWUKICAgICAgICAgICAgICAgIH0pLmFkZFRvKG1hcF9jNjIyYzNjNDQ3NDU0ZmUxYWFlZjgwNzRmNGMxYjhhNCk7CiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f4ef4a58>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 68
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "z3DcvfLXHCZz",
        "colab_type": "text"
      },
      "source": [
        "This example uses a Support Vector Machine (SVM) classifier ([Burges 1998](https://rd.springer.com/article/10.1023%2FA%3A1009715923555)). Note that the SVM is specified with a set of custom parameters. Without a priori information about the physical nature of the prediction problem, optimal parameters are unknown. See [Hsu et al. (2003)](https://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf) for a rough guide to choosing parameters for an SVM.\n",
        "\n",
        "To assess the accuracy of a classifier, use a `ConfusionMatrix` ([Stehman 1997](https://www.sciencedirect.com/science/article/abs/pii/S0034425797000837#aep-article-footnote-id1)). The following example uses `sample()` to generate training and validation data from a MODIS reference image and compares confusion matrices representing training and validation accuracy:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tnChq4QoIPXE",
        "colab_type": "code",
        "outputId": "8895c62e-3eb7-443a-d0b8-eed990112cbd",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 651
        }
      },
      "source": [
        "# Define a region of interest as a point.  Change the coordinates\n",
        "# to get a classification of any place where there is imagery.\n",
        "roi = ee.Geometry.Point(-122.3942, 37.7295)\n",
        "\n",
        "# Load Landsat 5 input imagery.\n",
        "# 1. Filter to get only one year of images.\n",
        "# 2. Filter to get only images under the region of interest.\n",
        "# 3. Sort by scene cloudiness, ascending.\n",
        "# 4. Get the first (least cloudy) scene.\n",
        "landsat = ee.Image(ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')\\\n",
        "                     .filterDate('2011-01-01', '2011-12-31')\\\n",
        "                     .filterBounds(roi)\\\n",
        "                     .sort('CLOUD_COVER')\\\n",
        "                     .first())\n",
        "\n",
        "# Compute cloud score.\n",
        "cloudScore = ee.Algorithms.Landsat.simpleCloudScore(landsat).select('cloud')\n",
        "\n",
        "# Mask the input for clouds.  Compute the min of the input mask to mask\n",
        "# pixels where any band is masked.  Combine that with the cloud mask.\n",
        "Input = landsat.updateMask(landsat.mask().reduce('min').And(cloudScore.lte(50)))\n",
        "\n",
        "# Use MODIS land cover, IGBP classification, for training.\n",
        "modis = ee.Image('MODIS/051/MCD12Q1/2011_01_01')\\\n",
        "          .select('Land_Cover_Type_1')\n",
        "\n",
        "# Sample the input imagery to get a FeatureCollection of training data.\n",
        "training = Input.addBands(modis).sample(numPixels=5000, seed=0)\n",
        "\n",
        "# Make a Random Forest classifier and train it.\n",
        "classifier = ee.Classifier.randomForest(10)\\\n",
        "               .train(training, 'Land_Cover_Type_1')\n",
        "\n",
        "# Classify the input imagery.\n",
        "classified = Input.classify(classifier)\n",
        "\n",
        "# Get a confusion matrix representing resubstitution accuracy.\n",
        "trainAccuracy = classifier.confusionMatrix()\n",
        "print('Resubstitution error matrix: ', trainAccuracy.getInfo())\n",
        "print('Training overall accuracy: ', trainAccuracy.accuracy().getInfo())\n",
        "\n",
        "# Sample the input with a different random seed to get validation data.\n",
        "validation = Input.addBands(modis)\\\n",
        "                  .sample(numPixels=5000,seed=1)\\\n",
        "                  .filter(ee.Filter.neq('B1', None)) # Filter the result to get rid of any null pixels.\n",
        "\n",
        "# Classify the validation data.\n",
        "validated = validation.classify(classifier)\n",
        "\n",
        "# Get a confusion matrix representing expected accuracy.\n",
        "testAccuracy = validated.errorMatrix('Land_Cover_Type_1', 'classification')\n",
        "print('Validation error matrix: ', testAccuracy.getInfo())\n",
        "print('Validation overall accuracy: ', testAccuracy.accuracy().getInfo())\n",
        "\n",
        "# Define a palette for the IGBP classification.\n",
        "igbpPalette = [\n",
        "  'aec3d4', # water\n",
        "  '152106', '225129', '369b47', '30eb5b', '387242', # forest\n",
        "  '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40',  # shrub, grass\n",
        "  '111149', # wetlands\n",
        "  'cdb33b', # croplands\n",
        "  'cc0013', # urban\n",
        "  '33280d', # crop mosaic\n",
        "  'd7cdcc', # snow and ice\n",
        "  'f7e084', # barren\n",
        "  '6f6f6f'  # tundra\n",
        "]\n",
        "\n",
        "# Display the input and the classification.\n",
        "Input_token = Input.getMapId({'bands': ['B3', 'B2', 'B1'], 'max': 0.4})\n",
        "classified_token = classified.getMapId({'palette': igbpPalette, 'min': 0, 'max': 17})\n",
        "center = [37.7295,-122.3942]\n",
        "Mapdisplay(center, {'Landsat':Input_token,'LandUse':classified_token}, zoom_start=10)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Resubstitution error matrix:  [[1763, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 2, 4, 0, 0, 0], [5, 197, 0, 0, 0, 2, 0, 0, 13, 3, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 13, 0, 0, 0, 68, 0, 0, 9, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 16, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 42, 8, 1, 2, 0, 0, 0, 0, 0, 0], [1, 10, 0, 0, 0, 2, 0, 0, 679, 7, 3, 0, 14, 3, 0, 0, 0], [1, 2, 0, 0, 0, 5, 0, 0, 30, 237, 5, 0, 5, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2, 23, 11, 276, 0, 9, 3, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 17, 5, 11, 0, 574, 4, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 2, 3, 2, 0, 16, 343, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 1, 0, 4, 0, 32, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6]]\n",
            "Training overall accuracy:  0.9369349503858876\n",
            "Validation error matrix:  [[1751, 0, 0, 0, 0, 0, 0, 0, 8, 0, 4, 0, 13, 9, 0, 0, 1], [8, 106, 0, 0, 0, 11, 0, 0, 63, 3, 0, 0, 4, 1, 0, 0, 0], [1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 14, 0, 0, 0, 10, 0, 0, 43, 3, 1, 0, 4, 1, 0, 0, 0], [3, 0, 0, 0, 0, 0, 1, 0, 9, 1, 0, 0, 4, 3, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2, 14, 5, 5, 0, 8, 4, 0, 0, 0], [9, 30, 0, 0, 0, 19, 2, 5, 442, 68, 49, 0, 86, 23, 0, 0, 0], [2, 3, 0, 0, 0, 3, 1, 2, 152, 65, 26, 0, 29, 3, 0, 0, 0], [3, 1, 0, 0, 0, 2, 0, 6, 111, 27, 109, 0, 84, 11, 0, 0, 0], [6, 1, 0, 0, 0, 0, 0, 1, 8, 0, 4, 1, 4, 1, 0, 0, 0], [5, 2, 0, 0, 0, 2, 0, 3, 138, 23, 56, 1, 386, 40, 1, 0, 0], [4, 0, 0, 0, 0, 3, 0, 2, 43, 4, 14, 0, 57, 236, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 18, 1, 10, 0, 13, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]\n",
            "Validation overall accuracy:  0.6774896491610373\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vzb21lLW1hcmtlcnMuanMiPjwvc2NyaXB0PgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmNzcyIvPgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL21heGNkbi5ib290c3RyYXBjZG4uY29tL2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xYTE4NzRiNTY0YTQzMjk5NTJlYTBjOWIxNGY4NDNiLmJhc2VfbGF5ZXJzLAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF81MWExODc0YjU2NGE0MzI5OTUyZWEwYzliMTRmODQzYi5vdmVybGF5cywKICAgICAgICAgICAgICAgIHtwb3NpdGlvbjogJ3RvcHJpZ2h0JywKICAgICAgICAgICAgICAgICBjb2xsYXBzZWQ6IHRydWUsCiAgICAgICAgICAgICAgICAgYXV0b1pJbmRleDogdHJ1ZQogICAgICAgICAgICAgICAgfSkuYWRkVG8obWFwX2VhY2RhZGJkMDcxMTRhNTE5NDA5ZjI0YzhlNTM2YmExKTsKICAgICAgICAgICAgCiAgICAgICAgCjwvc2NyaXB0Pg==\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f4e86e10>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 86
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9xfVZiZCPQVF",
        "colab_type": "text"
      },
      "source": [
        "This example uses a random forest ([Breiman 2001](https://rd.springer.com/article/10.1023/A:1010933404324)) classifier with 10 trees to downscale MODIS data to Landsat resolution. The `sample()` method generates two random samples from the MODIS data: one for training and one for validation. The training sample is used to train the classifier. You can get resubstitution accuracy on the training data from `classifier.confusionMatrix()`. To get validation accuracy, classify the validation data. This adds a `classification` property to the validation `FeatureCollection`. Call `errorMatrix()` on the classified `FeatureCollection` to get a confusion matrix representing validation (expected) accuracy.\n",
        "\n",
        "Inspect the output to see that the overall accuracy estimated from the training data is much higher than the validation data. The accuracy estimated from training data is an overestimate because the random forest is “fit” to the training data. The expected accuracy on unknown data is lower, as indicated by the estimate from the validation data."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4QC3gFjCP5-b",
        "colab_type": "text"
      },
      "source": [
        "# 2. Unsupervised Classification (clustering)\n",
        "\n",
        "The `ee.Clusterer` package handles unsupervised classification (or clustering) in Earth Engine. These algorithms are currently based on the algorithms with the same name in [Weka](http://www.cs.waikato.ac.nz/ml/weka/). \n",
        "\n",
        "Clusterers are used in the same manner as classifiers in Earth Engine. The general workflow for clustering is:\n",
        "\n",
        "1. Assemble features with numeric properties in which to find clusters.\n",
        "2. Instantiate a clusterer. Set its parameters if necessary.\n",
        "3. Train the clusterer using the training data.\n",
        "4. Apply the clusterer to an image or feature collection.\n",
        "5. Label the clusters.\n",
        "\n",
        "The training data is a `FeatureCollection` with properties that will be input to the clusterer. Unlike classifiers, there is no input class value for an `Clusterer`. Like classifiers, the data for the train and apply steps are expected to have the same number of values. When a trained clusterer is applied to an image or table, it assigns an integer cluster ID to each pixel or feature.\n",
        "\n",
        "Here is a simple example of building and using an `ee.Clusterer`:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yUqJ2a2yQRzi",
        "colab_type": "code",
        "outputId": "68050e76-6944-4404-8b11-9357ecdcfc1f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Load a pre-computed Landsat composite for input.\n",
        "Input = ee.Image('LANDSAT/LE7_TOA_1YEAR/2001')\n",
        "\n",
        "# Define a region in which to generate a sample of the input.\n",
        "region = ee.Geometry.Rectangle(29.7, 30, 32.5, 31.7)\n",
        "region_img = ee.Image().paint(region, 0, 2)\n",
        "# Display the sample region.\n",
        "center = [31.0,31.5]\n",
        "Mapdisplay(center,{'Region':region_img.getMapId()},zoom_start=8)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIg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cyYTA1YWU2MDExNGFkZTk3MTg1ODVjNDUwNTI5YjksIH0KICAgICAgICAgICAgICAgIH07CiAgICAgICAgICAgIEwuY29udHJvbC5sYXllcnMoCiAgICAgICAgICAgICAgICBsYXllcl9jb250cm9sXzU4YmJjMTIwZjI0ZTRiZWE4ZTQ1ZGJjZWYzOTQwZTYyLmJhc2VfbGF5ZXJzLAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF81OGJiYzEyMGYyNGU0YmVhOGU0NWRiY2VmMzk0MGU2Mi5vdmVybGF5cywKICAgICAgICAgICAgICAgIHtwb3NpdGlvbjogJ3RvcHJpZ2h0JywKICAgICAgICAgICAgICAgICBjb2xsYXBzZWQ6IHRydWUsCiAgICAgICAgICAgICAgICAgYXV0b1pJbmRleDogdHJ1ZQogICAgICAgICAgICAgICAgfSkuYWRkVG8obWFwXzViNjE0NmE1NTRiNzQ2OGI4NTYyY2RkYTliNzRiNGU0KTsKICAgICAgICAgICAgCiAgICAgICAgCjwvc2NyaXB0Pg==\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f4ea36d8>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 101
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XmPPaD1yRG3m",
        "colab_type": "code",
        "outputId": "4d8b97e1-2f45-49c6-b033-cfff6a34b17a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Make the training dataset.\n",
        "training = Input.sample(\n",
        "  region=region,\n",
        "  scale=30,\n",
        "  numPixels=5000\n",
        ")\n",
        "\n",
        "# Instantiate the clusterer and train it.\n",
        "clusterer = ee.Clusterer.wekaKMeans(15).train(training)\n",
        "\n",
        "# Cluster the input using the trained clusterer.\n",
        "result = Input.cluster(clusterer)\n",
        "result_tk = result.randomVisualizer().getMapId()\n",
        "# Display the clusters with random colors.\n",
        "Mapdisplay(center, {'results':result_tk},zoom_start=8)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vzb21lLW1hcmtlcnMuanMiPjwvc2NyaXB0PgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmNzcyIvP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pbGUub3BlbnN0cmVldG1hcC5vcmcve3p9L3t4fS97eX0ucG5nJywKICAgICAgICB7CiAgICAgICAgImF0dHJpYnV0aW9uIjogbnVsbCwKICAgICAgICAiZGV0ZWN0UmV0aW5hIjogZmFsc2UsCiAgICAgICAgIm1heE5hdGl2ZVpvb20iOiAxOCwKICAgICAgICAibWF4Wm9vbSI6IDE4LAogICAgICAgICJtaW5ab29tIjogMCwKICAgICAgICAibm9XcmFwIjogZmFsc2UsCiAgICAgICAgIm9wYWNpdHkiOiAxLAogICAgICAgICJzdWJkb21haW5zIjogImFiYyIsCiAgICAgICAgInRtcyI6IGZhbHNlCn0pLmFkZFRvKG1hcF8zY2YzZjljMDBiNjE0YjY3YWFhOThkY2YzMTJiMjY0Yyk7CiAgICB2YXIgdGlsZV9sYXllcl8yODI1MWZiNjU2YWE0NWIxYjRlODA1NmJlOTFiYmQ1NyA9IEwudGlsZUxheWVyKAogICAgICAgICdodHRwczovL2VhcnRoZW5naW5lLmdvb2dsZWFwaXMuY29tL21hcC8wM2VhMjczODVkNzllMmM3MzA4ZjM3ZDgzNzZjMjhiNC97en0ve3h9L3t5fT90b2tlbj1kOTFlZjU2ODZiMmZmMWZiYjY1ZmUzNDRmNGEyMDYyMCcsCiAgICAgICAgewogICAgICAgICJhdHRyaWJ1dGlvbiI6ICJHb29nbGUgRWFydGggRW5naW5lIiwKICAgICAgICAiZGV0ZWN0UmV0aW5hIjogZmFsc2UsCiAgICAgICAgIm1heE5hdGl2ZVpvb20iOiAxOCwKICAgICAgICAibWF4Wm9vbSI6IDE4LAogICAgICAgICJtaW5ab29tIjogMCwKICAgICAgICAibm9XcmFwIjogZmFsc2UsCiAgICAgICAgIm9wYWNpdHkiOiAxLAogICAgICAgICJzdWJkb21haW5zIjogImFiYyIsCiAgICAgICAgInRtcyI6IGZhbHNlCn0pLmFkZFRvKG1hcF8zY2YzZjljMDBiNjE0YjY3YWFhOThkY2YzMTJiMjY0Yyk7CiAgICAKICAgICAgICAgICAgdmFyIGxheWVyX2NvbnRyb2xfODc5N2I0MWRiZWYwNDkxNzk1Y2NhYjI5NGEyNzIxZTQgPSB7CiAgICAgICAgICAgICAgICBiYXNlX2xheWVycyA6IHsgIm9wZW5zdHJlZXRtYXAiIDogdGlsZV9sYXllcl8xYmViNjJiMDcxMDg0YzVjODQzOTA0ODRiYjIyNDBhNCwgfSwKICAgICAgICAgICAgICAgIG92ZXJsYXlzIDogeyAicmVzdWx0cyIgOiB0aWxlX2xheWVyXzI4MjUxZmI2NTZhYTQ1YjFiNGU4MDU2YmU5MWJiZDU3LCB9CiAgICAgICAgICAgICAgICB9OwogICAgICAgICAgICBMLmNvbnRyb2wubGF5ZXJzKAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF84Nzk3YjQxZGJlZjA0OTE3OTVjY2FiMjk0YTI3MjFlNC5iYXNlX2xheWVycywKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfODc5N2I0MWRiZWYwNDkxNzk1Y2NhYjI5NGEyNzIxZTQub3ZlcmxheXMsCiAgICAgICAgICAgICAgICB7cG9zaXRpb246ICd0b3ByaWdodCcsCiAgICAgICAgICAgICAgICAgY29sbGFwc2VkOiB0cnVlLAogICAgICAgICAgICAgICAgIGF1dG9aSW5kZXg6IHRydWUKICAgICAgICAgICAgICAgIH0pLmFkZFRvKG1hcF8zY2YzZjljMDBiNjE0YjY3YWFhOThkY2YzMTJiMjY0Yyk7CiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f362e908>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 104
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LaGFnLAxSa71",
        "colab_type": "text"
      },
      "source": [
        "Please note:\n",
        "\n",
        "- The same inputs should always produce the same outputs, but reordering the inputs can change the results.\n",
        "\n",
        "- Training with as few as 10 bands * 100k points can produce an Out Of Memory error.\n",
        "- Cobweb can can take a long time to finish and can produce a large number of clusters.\n",
        "- The output clusters and their IDs are dependent on the algorithm and inputs."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bntjKgrqUsq-",
        "colab_type": "text"
      },
      "source": [
        "# 3. Landsat Algorithms\n",
        "\n",
        "## Landsat collection structure\n",
        "\n",
        "The USGS has moved to using a collection-based strategy for producing Landsat scenes. On May 1st, 2017, they stopped producing old-style (pre-Collection) scenes and now are only producing Collection 1 versions.\n",
        "\n",
        "The USGS now produces data in 3 categories for each satellite:\n",
        "\n",
        "- Tier 1 (T1) - Data that meets geometric and radiometric quality requirements\n",
        "- Tier 2 (T2) - Data that doesn't meet the Tier 1 requirements\n",
        "- Real Time (RT) - Data that hasn't yet been evaluated (it takes as much as a month).\n",
        "\n",
        "In order to allow access to both the validated T1 data and the newest real-time data together, we've sorted the scenes into 3 collections (per each satellite) as follows:\n",
        "\n",
        "ID\t| Description\n",
        "------------|-----------------\n",
        "LANDSAT/LC08/C01/T1_RT\t| Landsat 8, Collection 1, Tier 1 + Real Time\n",
        "LANDSAT/LC08/C01/T1\t| Landsat 8, Collection 1, Tier 1 only\n",
        "LANDSAT/LC08/C01/T2\t| Landsat 8, Collection 1, Tier 2 only\n",
        "\n",
        "Newly acquired scenes are added to the T1_RT collection daily. Once an RT scene gets reprocessed and categorized as either T1 or T2, it will be removed from the T1_RT collection and the new version will be added to the appropriate collection(s). If your work is sensitive to removals or potentially mis-registered scenes, you might want to stick to the T1 collection, but in general, it's very uncommon that any misregistration is large enough to notice on newly acquired scenes.\n",
        "\n",
        "Each of these collections contains the raw data (i.e., scaled, at-sensor radiance). In addition, for each collection that contains T1 data, we are also providing a TOA collection containing the top-of-atmosphere reflectance. (e.g.: LANDSAT/LC08/C01/T1_RT_TOA).\n",
        "\n",
        "Finally, because pre-Collection 1 scenes are no longer being produced, we will not be able to update the \"old-style\" Landsat collections (e.g.: LANDSAT/LC8_L1T). We plan to keep these collections for a while even after we've obtained all of the C1 data, but they will eventually be phased out.\n",
        "\n",
        "### Landsat processing methods\n",
        "\n",
        "Earth Engine contains a variety of Landsat specific processing methods. Specifically, there are methods to compute at-sensor radiance, top-of-atmosphere (TOA) reflectance, surface reflectance (SR), cloud score and cloud-free composites.\n",
        "\n",
        "#### At-sensor radiance and TOA reflectance\n",
        "\n",
        "The ‘raw’ scenes in Earth Engine contain imagery with digital numbers (DNs) that represent scaled radiance. The conversion of DNs to at-sensor radiance is a linear transformation using coefficients stored in scene metadata ([Chander et al. 2009](https://www.sciencedirect.com/science/article/abs/pii/S0034425709000169)). The `ee.Algorithms.Landsat.calibratedRadiance()` method performs this conversion. Conversion to TOA (or at-sensor) reflectance is a linear transformation that accounts for solar elevation and seasonally variable Earth-Sun distance. The TOA conversion is handled by the `ee.Algorithms.Landsat.TOA()` method. The TOA method converts thermal bands to brightness temperature. See [Chander et al. (2009)](https://www.sciencedirect.com/science/article/abs/pii/S0034425709000169) (or [this USGS site](https://landsat.usgs.gov/using-usgs-landsat-8-product) for Landsat 8) for more information about computing TOA reflectance or brightness temperature. The following example shows conversion from raw data to radiance and TOA reflectance for a Landsat 8 image:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xMJnX3AzVB9h",
        "colab_type": "code",
        "outputId": "5cb9e70b-f3c6-4419-9117-0e494f8a7cf6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "center = [37.37, -122.04]\n",
        "\n",
        "# Load a raw Landsat scene and display it.\n",
        "raw = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318')\n",
        "raw_tk = raw.getMapId({'bands': ['B4', 'B3', 'B2'], 'min': 6000, 'max': 12000})\n",
        "\n",
        "# Convert the raw data to radiance.\n",
        "radiance = ee.Algorithms.Landsat.calibratedRadiance(raw)\n",
        "radiance_tk = radiance.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 90})\n",
        "\n",
        "# Convert the raw data to top-of-atmosphere reflectance.\n",
        "toa = ee.Algorithms.Landsat.TOA(raw)\n",
        "toa_tk = toa.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 0.2})\n",
        "\n",
        "#Display them!\n",
        "Mapdisplay(center,{'radiance':radiance_tk,'raw':raw_tk,'TOA':toa_tk})"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vz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gICAgICAgICAgbGF5ZXJfY29udHJvbF9iODhhYzgzYzNjNDk0ODRkOTMyZWEwY2Q4N2I0NWZlMi5vdmVybGF5cywKICAgICAgICAgICAgICAgIHtwb3NpdGlvbjogJ3RvcHJpZ2h0JywKICAgICAgICAgICAgICAgICBjb2xsYXBzZWQ6IHRydWUsCiAgICAgICAgICAgICAgICAgYXV0b1pJbmRleDogdHJ1ZQogICAgICAgICAgICAgICAgfSkuYWRkVG8obWFwX2Q4ZjVmMWFmMWM1YjRiOGI5NzdjM2IyZjdjOTkyMGVjKTsKICAgICAgICAgICAgCiAgICAgICAgCjwvc2NyaXB0Pg==\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f35f4978>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 126
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "F0rurogXZRpl",
        "colab_type": "text"
      },
      "source": [
        "### Surface reflectance\n",
        "\n",
        "Conversion to surface reflectance generally requires some sort of atmospheric compensation. USGS uses the LEDAPS software ([Schmidt et al. 2013](https://pubs.er.usgs.gov/publication/ofr20131057)) to convert raw Landsat data to surface reflectance. LEDAPS requires a variety of atmospheric inputs to constrain an atmospheric model. These include outputs of a [6S model](http://6s.ltdri.org/index.html) run, a DEM, total column ozone, National Centers for Environmental Prediction (NCEP) modeled surface pressure, temperature and water vapor ([Kalnay et al. 1996](https://journals.ametsoc.org/doi/abs/10.1175/1520-0477(1996)077%3C0437%3ATNYRP%3E2.0.CO%3B2)).\n",
        "\n",
        "You can access pre-computed surface reflectance images directly from the SR collections. For example, to load a Landsat 7 surface reflectance image, use:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gH6pdbVVZrDc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "srImage = ee.Image('LANDSAT/LE07/C01/T1_SR/LE07_044034_19990707')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1O0HlJi6Ztci",
        "colab_type": "text"
      },
      "source": [
        "The surface reflectance datasets for Collection 1 Landsat 4 through 7 are:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HgVXLw7XZsqP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "surfaceReflectanceL4 = ee.ImageCollection('LANDSAT/LT04/C01/T1_SR')\n",
        "surfaceReflectanceL5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')\n",
        "surfaceReflectanceL7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cMBgS4LiZ0nQ",
        "colab_type": "text"
      },
      "source": [
        "#### Simple cloud score\n",
        "\n",
        "For scoring Landsat pixels by their relative cloudiness, Earth Engine provides a rudimentary cloud scoring algorithm in the `ee.Algorithms.Landsat.simpleCloudScore()` method. The following example uses the cloud scoring algorithm to mask clouds in a Landsat 8 image:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xVbsBn2mZx2f",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "center = [37.37, -122.04]\n",
        "\n",
        "# Load a cloudy Landsat scene and display it.\n",
        "cloudy_scene = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140926')\n",
        "cloudy_tk=cloudy_scene.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 0.4})\n",
        "\n",
        "# Add a cloud score band.  It is automatically called 'cloud'.\n",
        "scored = ee.Algorithms.Landsat.simpleCloudScore(cloudy_scene)\n",
        "# Create a mask from the cloud score and combine it with the image mask.\n",
        "mask = scored.select(['cloud']).lte(20)\n",
        "\n",
        "# Apply the mask to the image and display the result.\n",
        "masked = cloudy_scene.updateMask(mask)\n",
        "masked_tk=masked.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 0.4})\n",
        "\n",
        "# Load a Landsat 8 composite and set the SENSOR_ID property.\n",
        "mosaic = ee.Image(ee.ImageCollection('LANDSAT/LC8_L1T_8DAY_TOA').first())\\\n",
        "           .set('SENSOR_ID', 'OLI_TIRS')\n",
        "\n",
        "# Cloud score the mosaic and display the result.\n",
        "scored_mosaic = ee.Algorithms.Landsat.simpleCloudScore(mosaic)\n",
        "mosaic_tk=scored_mosaic.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 0.4})\n",
        "\n",
        "Mapdisplay(center,{'TOA':cloudy_tk,'masked':masked_tk,'TOA mosaic':mosaic_tk})"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AAbKwxbfdrcb",
        "colab_type": "text"
      },
      "source": [
        "If you run this example in the Code Editor, try toggling the visibility of the TOA layers to compare the difference between the masked and unmasked imagery. (See [this section](https://developers.google.com/earth-engine/playground#layer-manager) of the Code Editor docs for instructions on how to do that). Observe that the input to `simpleCloudScore()` is a single Landsat TOA scene. Also note that `simpleCloudScore()` adds a band called `‘cloud’` to the input image. The cloud band contains the cloud score from 0 (not cloudy) to 100 (most cloudy). The previous example uses an arbitrary threshold (20) on the cloud score to mask cloudy pixels. To apply this algorithm to an Earth Engine mosaic of Landsat scenes, set the `SENSOR_ID` property:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GwW28lUVeA8H",
        "colab_type": "code",
        "outputId": "e341ab6c-66b7-4e61-f451-6a8351772a38",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Load a Landsat 8 composite and set the SENSOR_ID property.\n",
        "mosaic = ee.Image(ee.ImageCollection('LANDSAT/LC8_L1T_8DAY_TOA').first())\\\n",
        "           .set('SENSOR_ID', 'OLI_TIRS')\n",
        "\n",
        "# Cloud score the mosaic and display the result.\n",
        "scored_mosaic = ee.Algorithms.Landsat.simpleCloudScore(mosaic)\n",
        "mosaic_tk = scored_mosaic.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 0.4})\n",
        "\n",
        "Mapdisplay(center,{'TOA mosaic':mosaic_tk},zoom_start=5)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vzb21lLW1hcmtlcnMuanMiPjwvc2NyaXB0PgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xl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wudGlsZUxheWVyKAogICAgICAgICdodHRwczovL3tzfS50aWxlLm9wZW5zdHJlZXRtYXAub3JnL3t6fS97eH0ve3l9LnBuZycsCiAgICAgICAgewogICAgICAgICJhdHRyaWJ1dGlvbiI6IG51bGwsCiAgICAgICAgImRldGVjdFJldGluYSI6IGZhbHNlLAogICAgICAgICJtYXhOYXRpdmVab29tIjogMTgsCiAgICAgICAgIm1heFpvb20iOiAxOCwKICAgICAgICAibWluWm9vbSI6IDAsCiAgICAgICAgIm5vV3JhcCI6IGZhbHNlLAogICAgICAgICJvcGFjaXR5IjogMSwKICAgICAgICAic3ViZG9tYWlucyI6ICJhYmMiLAogICAgICAgICJ0bXMiOiBmYWxzZQp9KS5hZGRUbyhtYXBfMjNjZjQ0M2NjMWRhNDhlZDk5YTg1MDY0OWMwOThjZTgpOwogICAgdmFyIHRpbGVfbGF5ZXJfOGRjZTA2Y2MwZGFjNGM0MTgwMGRiZjZjNzcyNTUyNDIgPSBMLnRpbGVMYXllcigKICAgICAgICAnaHR0cHM6Ly9lYXJ0aGVuZ2luZS5nb29nbGVhcGlzLmNvbS9tYXAvMGE0MTFmMDQ1YjlhMzdiNWE5MjYxMGZiMDc4MDNmMjQve3p9L3t4fS97eX0/dG9rZW49MTY2NWE4YzMzNWNlMDRmOGZiOTk4YjQ5Y2YzM2NjYTUnLAogICAgICAgIHsKICAgICAgICAiYXR0cmlidXRpb24iOiAiR29vZ2xlIEVhcnRoIEVuZ2luZSIsCiAgICAgICAgImRldGVjdFJldGluYSI6IGZhbHNlLAogICAgICAgICJtYXhOYXRpdmVab29tIjogMTgsCiAgICAgICAgIm1heFpvb20iOiAxOCwKICAgICAgICAibWluWm9vbSI6IDAsCiAgICAgICAgIm5vV3JhcCI6IGZhbHNlLAogICAgICAgICJvcGFjaXR5IjogMSwKICAgICAgICAic3ViZG9tYWlucyI6ICJhYmMiLAogICAgICAgICJ0bXMiOiBmYWxzZQp9KS5hZGRUbyhtYXBfMjNjZjQ0M2NjMWRhNDhlZDk5YTg1MDY0OWMwOThjZTgpOwogICAgCiAgICAgICAgICAgIHZhciBsYXllcl9jb250cm9sX2UxMDgzNGQ3ZDdmNzRhNzRhMGYyZDEyZmNhNmJlMWM3ID0gewogICAgICAgICAgICAgICAgYmFzZV9sYXllcnMgOiB7ICJvcGVuc3RyZWV0bWFwIiA6IHRpbGVfbGF5ZXJfMDQxZDJhMDJkMDdjNDIxOGI3Zjg4NzNhMWIyZTY1MGMsIH0sCiAgICAgICAgICAgICAgICBvdmVybGF5cyA6IHsgIlRPQSBtb3NhaWMiIDogdGlsZV9sYXllcl84ZGNlMDZjYzBkYWM0YzQxODAwZGJmNmM3NzI1NTI0MiwgfQogICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgTC5jb250cm9sLmxheWVycygKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfZTEwODM0ZDdkN2Y3NGE3NGEwZjJkMTJmY2E2YmUxYzcuYmFzZV9sYXllcnMsCiAgICAgICAgICAgICAgICBsYXllcl9jb250cm9sX2UxMDgzNGQ3ZDdmNzRhNzRhMGYyZDEyZmNhNmJlMWM3Lm92ZXJsYXlzLAogICAgICAgICAgICAgICAge3Bvc2l0aW9uOiAndG9wcmlnaHQnLAogICAgICAgICAgICAgICAgIGNvbGxhcHNlZDogdHJ1ZSwKICAgICAgICAgICAgICAgICBhdXRvWkluZGV4OiB0cnVlCiAgICAgICAgICAgICAgICB9KS5hZGRUbyhtYXBfMjNjZjQ0M2NjMWRhNDhlZDk5YTg1MDY0OWMwOThjZTgpOwogICAgICAgICAgICAKICAgICAgICAKPC9zY3JpcHQ+\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f35fd630>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 152
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9OvoGlsbezD2",
        "colab_type": "text"
      },
      "source": [
        "`SENSOR_ID` is a property of individual images. When Earth Engine makes a mosaic of many images, it has to throw out individual image metadata, including the `SENSOR_ID` property. To cloud score a mosaic, Earth Engine looks for that property and can't find it, resulting in an error. Set the property manually to avoid that. The sensor IDs of Landsat 5, 7 and 8 are 'TM', 'ETM+' and 'OLI_TIRS', respectively.\n",
        "\n",
        "### Simple composite\n",
        "\n",
        "For creating simple cloud-free Landsat composites, Earth Engine provides the `ee.Algorithms.Landsat.simpleComposite()` method. This method selects a subset of scenes at each location, converts to TOA reflectance, applies the simple cloud score and takes the median of the least cloudy pixels. This example creates a simple composite using default parameters and compares it to a composite using custom parameters for the cloud score threshold and the percentile:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4NqSWTk6e4HG",
        "colab_type": "code",
        "outputId": "5b3f1cfe-d61e-486d-c8e1-a77314033cd5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Load a raw Landsat 5 ImageCollection for a single year.\n",
        "collection = ee.ImageCollection('LANDSAT/LT05/C01/T1')\\\n",
        "               .filterDate('2010-01-01', '2010-12-31')\n",
        "\n",
        "# Create a cloud-free composite with default parameters.\n",
        "composite = ee.Algorithms.Landsat.simpleComposite(collection)\n",
        "\n",
        "# Create a cloud-free composite with custom parameters for\n",
        "# cloud score threshold and percentile.\n",
        "customComposite = ee.Algorithms.Landsat.simpleComposite(\n",
        "  collection=collection,\n",
        "  percentile=75,\n",
        "  cloudScoreRange=5\n",
        ")\n",
        "\n",
        "# Display the composites.\n",
        "center=[37.7726,-122.3578]\n",
        "\n",
        "composite_tk=composite.getMapId({'bands': ['B4', 'B3', 'B2'], 'max': 128})\n",
        "customComposite_tk = customComposite.getMapId({'bands':['B4', 'B3', 'B2'], 'max':128})\n",
        "\n",
        "Mapdisplay(center=center,\n",
        "           dicc={'TOA composite':composite_tk,'Custom TOA composite':customComposite_tk},\n",
        "           zoom_start=10)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vzb21lLW1hcmtlcnMuanMiPjwvc2NyaXB0PgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmNzcyIvPgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL21heGNkbi5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ucG5nJywKICAgICAgICB7CiAgICAgICAgImF0dHJpYnV0aW9uIjogbnVsbCwKICAgICAgICAiZGV0ZWN0UmV0aW5hIjogZmFsc2UsCiAgICAgICAgIm1heE5hdGl2ZVpvb20iOiAxOCwKICAgICAgICAibWF4Wm9vbSI6IDE4LAogICAgICAgICJtaW5ab29tIjogMCwKICAgICAgICAibm9XcmFwIjogZmFsc2UsCiAgICAgICAgIm9wYWNpdHkiOiAxLAogICAgICAgICJzdWJkb21haW5zIjogImFiYyIsCiAgICAgICAgInRtcyI6IGZhbHNlCn0pLmFkZFRvKG1hcF80YTAzOWNjOTgyMDg0NDk5OGU4YTg5ZDUzOGVjNjIxYik7CiAgICB2YXIgdGlsZV9sYXllcl84NWI5YmUzMzMxY2E0MjlmODc1OGU1NzU4ZmM1ZjE0ZiA9IEwudGlsZUxheWVyKAogICAgICAgICdodHRwczovL2VhcnRoZW5naW5lLmdvb2dsZWFwaXMuY29tL21hcC82NjI2ZjcxYTkyNDNhYTBmYzgyYzg4NTdhODJjMjFhMy97en0ve3h9L3t5fT90b2tlbj1hN2IyNzU2NTNkZTg2ZmFmOGE4N2MyZWM1M2E5NDAyZScsCiAgICAgICAgewogICAgICAgICJhdHRyaWJ1dGlvbiI6ICJHb29nbGUgRWFydGggRW5naW5lIiwKICAgICAgICAiZGV0ZWN0UmV0aW5hIjogZmFsc2UsCiAgICAgICAgIm1heE5hdGl2ZVpvb20iOiAxOCwKICAgICAgICAibWF4Wm9vbSI6IDE4LAogICAgICAgICJtaW5ab29tIjogMCwKICAgICAgICAibm9XcmFwIjogZmFsc2UsCiAgICAgICAgIm9wYWNpdHkiOiAxLAogICAgICAgICJzdWJkb21haW5zIjogImFiYyIsCiAgICAgICAgInRtcyI6IGZhbHNlCn0pLmFkZFRvKG1hcF80YTAzOWNjOTgyMDg0NDk5OGU4YTg5ZDUzOGVjNjIxYik7CiAgICB2YXIgdGlsZV9sYXllcl9lNmMwNTlmZTg5ZWY0ZWM1ODlkMmY4NWM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2wubGF5ZXJzKAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF83YmI5MWU1YzZlN2Q0YjlmOGFkOWVkZTJmNjY3YmUyMi5iYXNlX2xheWVycywKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfN2JiOTFlNWM2ZTdkNGI5ZjhhZDllZGUyZjY2N2JlMjIub3ZlcmxheXMsCiAgICAgICAgICAgICAgICB7cG9zaXRpb246ICd0b3ByaWdodCcsCiAgICAgICAgICAgICAgICAgY29sbGFwc2VkOiB0cnVlLAogICAgICAgICAgICAgICAgIGF1dG9aSW5kZXg6IHRydWUKICAgICAgICAgICAgICAgIH0pLmFkZFRvKG1hcF80YTAzOWNjOTgyMDg0NDk5OGU4YTg5ZDUzOGVjNjIxYik7CiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f35fdf60>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 162
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "frxN8a6imVOp",
        "colab_type": "text"
      },
      "source": [
        "Note that the input to the simple composite is a collection of raw imagery. Also note that by default, reflective band output is reflectance scaled to 8-bits and thermal band output is Kelvin minus 100, to fit in the 8-bit range. You can change this behavior by setting the `asFloat` parameter to true, to get un-scaled, un-shifted float output."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FU-YCoyDmred",
        "colab_type": "text"
      },
      "source": [
        "# 4. Sentinel-1 Algorithms\n",
        "\n",
        "To create a homogeneous subset of Sentinel-1 data, it will usually be necessary to filter the collection using metadata properties. The common metadata fields used for filtering include these properties:\n",
        "\n",
        "1. `transmitterReceiverPolarisation`:  ['VV'], ['HH'], ['VV', 'VH'], or ['HH', 'HV']\n",
        "\n",
        "2. instrumentMode: 'IW' (Interferometric Wide Swath), 'EW' (Extra Wide Swath) or 'SM' (Strip Map). See [this reference](https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/acquisition-modes) for details.\n",
        "\n",
        "3. orbitProperties_pass: 'ASCENDING' or 'DESCENDING'.\n",
        "\n",
        "4. resolution_meters: 10, 25 or 40\n",
        "\n",
        "5. resolution: 'M' (medium) or 'H' (high). See [this reference](https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/resolutions/level-1-ground-range-detected) for details.\n",
        "\n",
        "For example:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-W32sKgInMz7",
        "colab_type": "code",
        "outputId": "2b7e6030-1c5f-47dd-83f9-eb410c8cd8b0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Load the Sentinel-1 ImageCollection.\n",
        "sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD')\\\n",
        "              .filterDate('2015-01-01','2015-03-01')\n",
        "\n",
        "# Filter by metadata properties.\n",
        "# 1. Filter to get images with VV and VH dual polarization.\n",
        "# 2. Filter to get images collected in interferometric wide swath mode.\n",
        "\n",
        "vh = sentinel1.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))\\\n",
        "              .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))\\\n",
        "              .filter(ee.Filter.eq('instrumentMode', 'IW'))\n",
        "\n",
        "# Filter to get images from different look angles.\n",
        "vhAscending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'ASCENDING'))\n",
        "vhDescending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'))\n",
        "\n",
        "# Create a composite from means at different polarizations and look angles.\n",
        "composite = ee.Image.cat([\n",
        "  vhAscending.select('VH').mean(),\n",
        "  ee.ImageCollection(vhAscending.select('VV').merge(vhDescending.select('VV'))).mean(),\n",
        "  vhDescending.select('VH').mean()\n",
        "]).focal_median()\n",
        "\n",
        "# Display as a composite of polarization and backscattering characteristics.\n",
        "center=[37.7726,-122.3578]\n",
        "composite_tk = composite.getMapId({'min': [-25, -20, -25], 'max': [0, 10, 0]})\n",
        "\n",
        "Mapdisplay(center,{'composite':composite_tk},zoom_start=3)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vzb21lLW1hcmtlcnMuanMiPjwvc2NyaXB0PgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL2Nkbi5qc2R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UKfSkuYWRkVG8obWFwXzg2ZTVhMDM5YTBlNDQzMzI5MDliYjg5MmU5ZWEzYzhkKTsKICAgIAogICAgICAgICAgICB2YXIgbGF5ZXJfY29udHJvbF9jOTgzMTQwM2QwYjc0NTMzYTQyYjMyMjU3MWQ5MzM0ZiA9IHsKICAgICAgICAgICAgICAgIGJhc2VfbGF5ZXJzIDogeyAib3BlbnN0cmVldG1hcCIgOiB0aWxlX2xheWVyX2UxMWI3NmI5YWEyNTRkOWQ4NzcwNDMyMjBhOGM0ZjI0LCB9LAogICAgICAgICAgICAgICAgb3ZlcmxheXMgOiB7ICJjb21wb3NpdGUiIDogdGlsZV9sYXllcl83YWVhMTVhZTZhYWI0ZWM5OTdiY2NhZTk4NDA0M2Q0ZiwgfQogICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgTC5jb250cm9sLmxheWVycygKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfYzk4MzE0MDNkMGI3NDUzM2E0MmIzMjI1NzFkOTMzNGYuYmFzZV9sYXllcnMsCiAgICAgICAgICAgICAgICBsYXllcl9jb250cm9sX2M5ODMxNDAzZDBiNzQ1MzNhNDJiMzIyNTcxZDkzMzRmLm92ZXJsYXlzLAogICAgICAgICAgICAgICAge3Bvc2l0aW9uOiAndG9wcmlnaHQnLAogICAgICAgICAgICAgICAgIGNvbGxhcHNlZDogdHJ1ZSwKICAgICAgICAgICAgICAgICBhdXRvWkluZGV4OiB0cnVlCiAgICAgICAgICAgICAgICB9KS5hZGRUbyhtYXBfODZlNWEwMzlhMGU0NDMzMjkwOWJiODkyZTllYTNjOGQpOwogICAgICAgICAgICAKICAgICAgICAKPC9zY3JpcHQ+\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f3550fd0>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 166
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RPn_S8Ccqvy0",
        "colab_type": "text"
      },
      "source": [
        "### Sentinel-1 Preprocessing\n",
        "Imagery in the Earth Engine `'COPERNICUS/S1_GRD'` Sentinel-1 `ImageCollection` is consists of Level-1 Ground Range Detected (GRD) scenes processed to backscatter coefficient (σ°) in decibels (dB). The backscatter coefficient represents target backscattering area (radar cross-section) per unit ground area. Because it can vary by several orders of magnitude, it is converted to dB as 10*log10σ°. It measures whether the radiated terrain scatters the incident microwave radiation preferentially away from the SAR sensor dB < 0) or towards the SAR sensor dB > 0). This scattering behavior depends on the physical characteristics of the terrain, primarily the geometry of the terrain elements and their electromagnetic characteristics.\n",
        "\n",
        "Earth Engine uses the following preprocessing steps (as implemented by the [Sentinel-1 Toolbox](https://sentinel.esa.int/web/sentinel/toolboxes/sentinel-1)) to derive the backscatter coefficient in each pixel:\n",
        "\n",
        "1. **Apply orbit file**\n",
        "- Updates orbit metadata with a restituted orbit file.\n",
        "\n",
        "2. **GRD border noise removal**\n",
        "- Removes low intensity noise and invalid data on scene edges. (As of January 12, 2018)\n",
        "\n",
        "3. **Thermal noise removal**\n",
        "- Removes additive noise in sub-swaths to help reduce discontinuities between sub-swaths for scenes in multi-swath acquisition modes. (This operation cannot be applied to images produced before July 2015)\n",
        "\n",
        "4. **Radiometric calibration**\n",
        "- Computes backscatter intensity using sensor calibration parameters in the GRD metadata.\n",
        "\n",
        "5. **Terrain correction (orthorectification)**\n",
        "- Converts data from ground range geometry, which does not take terrain into account, to σ° using the [SRTM 30 meter DEM](https://developers.google.com/earth-engine/datasets/catalog/USGS_SRTMGL1_003) or the [ASTER DEM](https://asterweb.jpl.nasa.gov/gdem.asp) for high latitudes (greater than 60° or less than -60°).\n",
        "\n",
        "### Dataset Notes\n",
        "\n",
        "- Radiometric Terrain Flattening is not being applied due to artifacts on mountain slopes.\n",
        "- The unitless backscatter coefficient is converted to dB as described above.\n",
        "- Sentinel-1 SLC data cannot currently be ingested, as Earth Engine does not support images with complex values due to inability to average them during pyramiding without losing phase information.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TAnqzBVqseAL",
        "colab_type": "text"
      },
      "source": [
        "# 5. Resampling and Reducing Resolution\n",
        "\n",
        "As noted in the [Projections](https://developers.google.com/earth-engine/projections) doc, Earth Engine performs nearest neighbor resampling by default during reprojection. You can change this behavior with the `resample()` or `reduceResolution()` methods. Specifically, when one of these methods is applied to an input image, any required reprojection of the input will be done using the indicated resampling or aggregation method.\n",
        "\n",
        "## Resampling\n",
        "\n",
        "`resample()` causes the indicated resampling method (`'bilinear'` or `'bicubic'`) to be used at the next reprojection. Since inputs are requested in the output projection, an implicit reprojection may happen before any other operation on the input. For this reason, call `resample()` directly on the input image. Consider the following simple example:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9KVYihHDs0UH",
        "colab_type": "code",
        "outputId": "b9bc6472-765e-48f4-ad75-ca848de7109d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Load a Landsat image over San Francisco, California, UAS.\n",
        "landsat = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20160323')\n",
        "# Force the next reprojection on this image to use bicubic resampling.\n",
        "resampled = landsat.resample('bicubic')\n",
        "\n",
        "# Set display and visualization parameters.\n",
        "center = [37.6193,-122.37383]\n",
        "visParams = {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}\n",
        "\n",
        "resampled_tk = resampled.getMapId(visParams)\n",
        "landsat_tk = landsat.getMapId(visParams)\n",
        "\n",
        "# Display the Landsat image using the default nearest neighbor and bicubic resampling.\n",
        "# when reprojecting to Mercator for the Code Editor map.\n",
        "Mapdisplay(center,{'raw':landsat_tk,'resampled':resampled_tk},zoom_start=15)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><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GFmZjdhIHsKICAgICAgICBwb3NpdGlvbjogcmVsYXRpdmU7CiAgICAgICAgd2lkdGg6IDEwMC4wJTsKICAgICAgICBoZWlnaHQ6IDEwMC4wJTsKICAgICAgICBsZWZ0OiAwLjAlOwogICAgICAgIHRvcDogMC4wJTsKICAgICAgICB9CiAgICA8L3N0eWxlPgo8L2hlYWQ+Cjxib2R5PiAgICAKICAgIAogICAgPGRpdiBjbGFzcz0iZm9saXVtLW1hcCIgaWQ9Im1hcF9hZDVkYjdiNjBiNzc0Zjg3OTE4MmZhNjE3NjRhZmY3YSIgPjwvZGl2Pgo8L2JvZHk+CjxzY3JpcHQ+ICAgIAogICAgCiAgICAKICAgICAgICB2YXIgYm91bmRzID0gbnVsbDsKICAgIAoKICAgIHZhciBtYXBfYWQ1ZGI3YjYwYjc3NGY4NzkxODJmYTYxNzY0YWZmN2EgPSBMLm1hcCgKICAgICAgICAnbWFwX2FkNWRiN2I2MGI3NzRmODc5MTgyZmE2MTc2NGFmZjdhJywgewogICAgICAgIGNlbnRlcjogWzM3LjYxOTMsIC0xMjIuMzczODNdLAogICAgICAgIHpvb206IDE1LAogICAgICAgIG1heEJvdW5kczogYm91bmRzLAogICAgICAgIGxheWVyczogW10sCiAgICAgICAgd29ybGRDb3B5SnVtcDogZmFsc2UsCiAgICAgICAgY3JzOiBMLkNSUy5FUFNHMzg1NywKICAgICAgICB6b29tQ29udHJvbDogdHJ1ZSwKICAgICAgICB9KTsKCgogICAgCiAgICB2YXIgdGlsZV9sYXllcl9iZmMwZDVlZTAyNWU0ZWI1OWU3YzVlMTczYjA1ZDAzNiA9IEwudGlsZUxheWVyKAogICAgICAgICdodHRwczovL3tzfS50aWxlLm9wZW5zdHJlZXRtYXAub3JnL3t6fS97eH0ve3l9LnBuZycsCiAgICAgICAgewogICAgICAgICJhdHRyaWJ1dGlvbiI6IG51bGwsCiAgICAgICAgImRldGVjdFJldGluYSI6IGZhbHNlLAogICAgICAgICJtYXhOYXRpdmVab29tIjogMT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ICAgICBsYXllcl9jb250cm9sXzA3MjQ0MjhmMGVjNjQ4N2VhNzRhNWMyODMxMzFiOWJiLm92ZXJsYXlzLAogICAgICAgICAgICAgICAge3Bvc2l0aW9uOiAndG9wcmlnaHQnLAogICAgICAgICAgICAgICAgIGNvbGxhcHNlZDogdHJ1ZSwKICAgICAgICAgICAgICAgICBhdXRvWkluZGV4OiB0cnVlCiAgICAgICAgICAgICAgICB9KS5hZGRUbyhtYXBfYWQ1ZGI3YjYwYjc3NGY4NzkxODJmYTYxNzY0YWZmN2EpOwogICAgICAgICAgICAKICAgICAgICAKPC9zY3JpcHQ+\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f35ad748>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 171
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GkC4vdD3uoaL",
        "colab_type": "text"
      },
      "source": [
        "The order of operations for this code sample is diagrammed in Figure 1. Specifically, the implicit reprojection to the [maps mercator](http://epsg.io/3857) projection takes place with the resampling method specified on the input image.\n",
        "\n",
        "<Figure>\n",
        "<center>\n",
        "  <img src=\"https://developers.google.com/earth-engine/images/Resample.png\">\n",
        "</center>\n",
        "</Figure>\n",
        "\n",
        "Figure 1. Flow chart of operations when `resample()` is called on the input image prior to display in the Code Editor. Curved lines indicate the flow of information to the reprojection: specifically, the output projection, scale and resampling method to use.\n",
        "\n",
        "### Reduce Resolution\n",
        "\n",
        "Suppose that instead of resampling during reprojection, your goal is to aggregate pixels to larger pixels in a different projection. This is useful when comparing image datasets at different scales, for example 30-meter pixels from a Landsat-based product to coarse pixels (higher scale) from a MODIS-based product. You can control this aggregation process with the `reduceResolution()` method. As with `resample()`, call `reduceResolution()` on the input, in order to affect the next reprojection of the image. The following example uses `reduceResolution()` to compare forest cover data at 30-meters resolution to a vegetation index at 500-meters resolution:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sL8jeIjVvQ3O",
        "colab_type": "code",
        "outputId": "2e8893fd-d741-4038-a332-840e7983a466",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 721
        }
      },
      "source": [
        "from pprint import pprint\n",
        "# Load a MODIS EVI image.\n",
        "modis = ee.Image(ee.ImageCollection('MODIS/006/MOD13A1').first())\\\n",
        "          .select('EVI')\n",
        "\n",
        "# Display the EVI image near La Honda, California.\n",
        "center = [37.5331, -122.3616]\n",
        "\n",
        "# Get information about the MODIS projection.\n",
        "modisProjection = modis.projection()\n",
        "\n",
        "print('MODIS projection:')\n",
        "pprint(modisProjection.getInfo())\n",
        "\n",
        "# Load and display forest cover data at 30 meters resolution.\n",
        "forest = ee.Image('UMD/hansen/global_forest_change_2015')\\\n",
        "           .select('treecover2000')\n",
        "\n",
        "# Get the forest cover data at MODIS scale and projection.\n",
        "# Force the next reprojection to aggregate instead of resampling.\n",
        "# Request the data at the scale and projection of the MODIS image.\n",
        "forestMean = forest.reduceResolution(reducer=ee.Reducer.mean(),\n",
        "                                     maxPixels=1024)\\\n",
        "                   .reproject(crs=modisProjection)\n",
        "\n",
        "# Display the aggregated, reprojected forest cover data.\n",
        "forest_viz = {'max': 80}\n",
        "modis_viz = {'min': 2000, 'max': 5000}\n",
        "\n",
        "modis_tk = modis.getMapId(modis_viz)\n",
        "forest_tk = forest.getMapId(forest_viz)\n",
        "forestMean_tk = forestMean.getMapId(forest_viz)\n",
        "\n",
        "Mapdisplay(center=center,\n",
        "           dicc = {'forest cover 30 m':forest_tk,\n",
        "                   'forest cover at MODIS scale':forestMean_tk,\n",
        "                   'MODIS EVI':modis_tk},\n",
        "           zoom_start=12)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "MODIS projection:\n",
            "{'crs': 'SR-ORG:6974',\n",
            " 'transform': [463.312716528,\n",
            "               0.0,\n",
            "               -20015109.354,\n",
            "               0.0,\n",
            "               -463.312716527,\n",
            "               10007554.677],\n",
            " 'type': 'Projection'}\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5jbG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvTGVhZmxldC5hd2Vzb21lLW1hcmtlcnMvMi4wLjIvbGVhZmxldC5hd2Vzb21lLW1hcmtlcnMuanMiPjwvc2NyaXB0PgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiBocmVmPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmNzcyIvPgogICAgPGxpbm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AgY29sbGFwc2VkOiB0cnVlLAogICAgICAgICAgICAgICAgIGF1dG9aSW5kZXg6IHRydWUKICAgICAgICAgICAgICAgIH0pLmFkZFRvKG1hcF80ZmZkNDU5Yzc2YzA0ZmUxYTYyMmEwY2NhNWIzZGZjMyk7CiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f354c8d0>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 177
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "46c7pSYezJ2F",
        "colab_type": "text"
      },
      "source": [
        "In this example, note that the output projection is explicitly set with [reproject()](https://developers.google.com/earth-engine/projections#reprojecting). During the reprojection to the MODIS sinusoidal projection, rather than resampling, the smaller pixels are aggregated with the specified reducer (`ee.Reducer.mean()` in the example). This sequence of operations is illustrated in Figure 2. Although this example uses `reproject()` to help visualize the effect of `reduceResolution()`, most scripts don't need to explicitly reproject; see the warning [here](https://developers.google.com/earth-engine/projections#reprojecting).\n",
        "\n",
        "<center>\n",
        "<img src='https://developers.google.com/earth-engine/images/ReduceResolution.png'>\n",
        "</center>\n",
        "\n",
        "Figure 2. Flow chart of operations when `reduceResolution()` is called on an input image prior to `reproject()`. Curved lines indicate the flow of information to the reprojection: specifically, the output projection, scale and pixel aggregation method to use.\n",
        "\n",
        "\n",
        "Note that a second reprojection occurs (implicitly) to display the data on the Code Editor map. Visually inspect the results and observe the correspondence between the pixels from the MODIS layer and the forest cover data reprojected to MODIS scale and projection. In general, you should rarely need to explicitly reproject() in Earth Engine.\n",
        "\n",
        "#### Pixel weights for ReduceResolution\n",
        "\n",
        "The weights of pixels used during the reduceResolution() aggregation process are based on the overlap between the smaller pixels being aggregated and the larger pixels specified by the output projection. This is illustrated in Figure 3.\n",
        "\n",
        "<center>\n",
        "<img src=\"https://developers.google.com/earth-engine/images/ReduceResolution_weights.png\">\n",
        "</center>\n",
        "\n",
        "Figure 3. Input pixels (black) and output pixel (blue) for `reduceResolution()`.\n",
        "\n",
        "The default behavior is that input pixel weights are computed as the fraction of the output pixel area covered by the input pixel. In the diagram, the output pixel has area a, the weight of the input pixel with intersection area b is computed as b/a and the weight of the input pixel with intersection area c is computed as c/a. This behavior can result in unexpected results when using a reducer other than the mean reducer. For example, to compute forested area per pixel, use the mean reducer to compute the fraction of a pixel covered, then multiply by area (instead of computing areas in the smaller pixels then adding them up with the sum reducer):\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lipKeBS8zv7C",
        "colab_type": "code",
        "outputId": "59cb2d94-f4ec-49e8-9955-3755d647e994",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "# Compute forest area per MODIS pixel.\n",
        "# 1.Force the next reprojection to aggregate instead of resampling.\n",
        "# 2. The reduce resolution returns the fraction of the MODIS pixel\n",
        "#    that's covered by 30 meter forest pixels.  Convert to area\n",
        "#    after the reduceResolution() call.\n",
        "# 3. Request the data at the scale and projection of the MODIS image.\n",
        "forestArea = forest.gt(0)\\\n",
        "                   .reduceResolution(reducer=ee.Reducer.mean(),\n",
        "                                         maxPixels=1024)\\\n",
        "                   .multiply(ee.Image.pixelArea())\\\n",
        "                   .reproject(crs=modisProjection)\n",
        "forest_viz = {'max': 500 * 500}\n",
        "forestArea_tk = forestArea.getMapId(forest_viz)\n",
        "\n",
        "Mapdisplay(center=center,\n",
        "           dicc={'forested area at MODIS scale':forestArea_tk})"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImN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YXlzIDogeyAiZm9yZXN0ZWQgYXJlYSBhdCBNT0RJUyBzY2FsZSIgOiB0aWxlX2xheWVyXzUwNTRkNGUzNjBhZDQ0MzNhMmZmNGI2NTAzOGU2YmY4LCB9CiAgICAgICAgICAgICAgICB9OwogICAgICAgICAgICBMLmNvbnRyb2wubGF5ZXJzKAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF8wMDZiZDExMDlmYTc0ZTg5YTA1NzIwMmMwNDczMTdmZS5iYXNlX2xheWVycywKICAgICAgICAgICAgICAgIGxheWVyX2NvbnRyb2xfMDA2YmQxMTA5ZmE3NGU4OWEwNTcyMDJjMDQ3MzE3ZmUub3ZlcmxheXMsCiAgICAgICAgICAgICAgICB7cG9zaXRpb246ICd0b3ByaWdodCcsCiAgICAgICAgICAgICAgICAgY29sbGFwc2VkOiB0cnVlLAogICAgICAgICAgICAgICAgIGF1dG9aSW5kZXg6IHRydWUKICAgICAgICAgICAgICAgIH0pLmFkZFRvKG1hcF9lZjU3YTkxYzA5NjE0YzhiYmQ5MTVmYjkyYjg3N2ZhNSk7CiAgICAgICAgICAgIAogICAgICAgIAo8L3NjcmlwdD4=\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f35f34a3470>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 182
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UAxgYF96kjTD",
        "colab_type": "text"
      },
      "source": [
        "<!--NAVIGATION-->\n",
        " < [Array](8_Array.ipynb) | [Contents](index.ipynb) |  [Asset Management](10_Import.ipynb)>\n",
        "\n",
        "<a href=\"https://colab.research.google.com/github/csaybar/EEwPython/blob/master/9_SpecializedAlgorithms.ipynb\"><img align=\"left\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\" title=\"Open and Execute in Google Colaboratory\"></a>"
      ]
    }
  ]
}